<?php
//
// 通用函数类库
// $Id: function.class.php 16 2012-12-03 04:37:30Z jiangbianwanghai@gmail.com $
//
class common{
	/**
	 * 将一个字串中含有全角的数字字符、字母、空格或'%+-()'字符转换为相应半角字符
	 *
	 * @access public
	 * @param string $str 待转换字串
	 *
	 * @return string $str 处理后字串
	 */
	public function make_semiangle($str)
	{
		$arr = array(
			'０' => '0', '１' => '1', '２' => '2', '３' => '3', '４' => '4',
			'５' => '5', '６' => '6', '７' => '7', '８' => '8', '９' => '9',
			'Ａ' => 'A', 'Ｂ' => 'B', 'Ｃ' => 'C', 'Ｄ' => 'D', 'Ｅ' => 'E',
			'Ｆ' => 'F', 'Ｇ' => 'G', 'Ｈ' => 'H', 'Ｉ' => 'I', 'Ｊ' => 'J',
			'Ｋ' => 'K', 'Ｌ' => 'L', 'Ｍ' => 'M', 'Ｎ' => 'N', 'Ｏ' => 'O',
			'Ｐ' => 'P', 'Ｑ' => 'Q', 'Ｒ' => 'R', 'Ｓ' => 'S', 'Ｔ' => 'T',
			'Ｕ' => 'U', 'Ｖ' => 'V', 'Ｗ' => 'W', 'Ｘ' => 'X', 'Ｙ' => 'Y',
			'Ｚ' => 'Z', 'ａ' => 'a', 'ｂ' => 'b', 'ｃ' => 'c', 'ｄ' => 'd',
			'ｅ' => 'e', 'ｆ' => 'f', 'ｇ' => 'g', 'ｈ' => 'h', 'ｉ' => 'i',
			'ｊ' => 'j', 'ｋ' => 'k', 'ｌ' => 'l', 'ｍ' => 'm', 'ｎ' => 'n',
			'ｏ' => 'o', 'ｐ' => 'p', 'ｑ' => 'q', 'ｒ' => 'r', 'ｓ' => 's',
			'ｔ' => 't', 'ｕ' => 'u', 'ｖ' => 'v', 'ｗ' => 'w', 'ｘ' => 'x',
			'ｙ' => 'y', 'ｚ' => 'z',
			'（' => '(', '）' => ')', '〔' => '[', '〕' => ']', '【' => '[',
			'】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']',
			'‘' => '[', '’' => ']', '｛' => '{', '｝' => '}', '《' => '<',
			'》' => '>',
			'％' => '%', '＋' => '+', '—' => '-', '－' => '-', '～' => '-',
			'：' => ':', '。' => '.', '、' => ',', '，' => '.', '、' => '.',
			'；' => ',', '？' => '?', '！' => '!', '…' => '-', '‖' => '|',
			'”' => '"', '’' => '`', '‘' => '`', '｜' => '|', '〃' => '"',
			'　' => ' '
		);
		return strtr($str, $arr);
	}
	
	/**
	 * 筛选出来一些优质的关键词，按照优质程度排列：纯汉字、汉字+英文组合、汉字+数字组合、纯英文、英文+数字组合、汉字+英文+数字组合
	 *
	 * @access public
	 * @param string $str 待转换字串
	 *
	 * @return string $str 处理后字串
	 */
	function getStrType($str)
	{
		if (preg_match("/^[0-9]+$/i", $str)) {
			return 1; // 纯数字
		}
		if (preg_match("/^[a-zA-Z]+$/i", $str)) {
			return 2; // 纯英文
		}
		if (preg_match("/^[\x7f-\xff]+$/i", $str)) {
			return 3; // 是否为汉字
		}
		$mb = mb_strlen($str,'utf-8');
		$st = strlen($str);
		if ($st == $mb) {
			if(preg_match("/^[0-9a-zA-Z]+$/i",$str)) {
				return 4; // 英文数字混合
			}
		} else {
			
			if (preg_match("/^[\x7f-\xff0-9]+$/i", $str)) {
				return 5; // 汉字数字混合
			} elseif (preg_match("/^[\x7f-\xffa-zA-Z]+$/i", $str)) {
				return 6; // 汉字字母混合
			} elseif (preg_match("/^[\x7f-\xff0-9a-zA-Z]+$/i", $str)) {
				return 7; // 汉字英文数字混合
			}
		}
		return 0;
	}
	
	//获取友好的倒计时时间
	function get_firend_time($time_str) {
		$string = '';
		$time = $time_str - time();
		//获得天数
		if ($time) {
			$day = floor($time/86400);
			if ($day) {
				$time = $time - $day*86400;
				$string .= $day.'天';
			}
		}
		//获得小时数
		if ($time) {
			$hour = floor($time/3600);
			if ($hour) {
				$time = $time - $hour*3600;
				$string .= $hour.'小时';
			}
		}
		//获得分钟数
		if ($time) {
			$minute = floor($time/60);
			if ($hour) {
				$time = $time - $minute*60;
				$string .= $minute.'分钟';
			}
		}
		if ($time) {
			$string .= $time.'秒';
		}
		return $string;
	}
}